//
//  CButtonUI.cpp
//  beast_tamer
//
//  Created by chiawei.tseng on 13/2/6.
//
//

#include "CButtonUI.h"
#include "CInputSystem.h"
#include "CMemberCallback.h"


CButtonUI* CButtonUI::CreateButtonUI(const char *spName , const fPoint &point , INPUT_ID id)
{
   
    CButtonUI *ptr = new CButtonUI();
    
    if (ptr && ptr->initWithFile(spName) && ptr->init())
    {
        ptr->InitWithData(point,id);
        ptr->autorelease();
        
        return ptr;
    }
    
    CC_SAFE_DELETE(ptr);
    
    return NULL;
}


CButtonUI::CButtonUI()
{
    
}

CButtonUI::~CButtonUI()
{
    
}

void CButtonUI::onEnter()
{
    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,true);
    
    CCSprite::onEnter();
}

void CButtonUI::onExit()
{
    CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
    
    CCSprite::onExit();
}

bool CButtonUI::init()
{
    if(!CBaseUI::init())
        return false;
    
    m_UiType = UI_BUTTON;
    
    //CInputSystem::Get().AddListen(this);
    if (CInputSystem::Exists())
    {
        CInputSystem::Get().AddAction(this);
        
        return true;
    }
    
    return false;
}

void CButtonUI::InitWithData(const fPoint &point , INPUT_ID id)
{
    m_Data = new BUTTON_INPUT_DATA();
    
    m_Data->m_inputID = id;
    
    CMemberCallback<CButtonUI>* memberCallBack = new CMemberCallback<CButtonUI>(this,&CButtonUI::HandlResult);
    
    m_Data->m_pCallBackPtr = static_cast<CCallBack*>(memberCallBack);
    
    setPosition(ccp(point.X,point.Y));
}

void CButtonUI::HandlResult(void* data)
{
    
}

void CButtonUI::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{
    m_bTriggered = true;
}
